package com.xiaofei.Filter;

import com.xiaofei.Tools.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class interceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String URI = request.getRequestURI();
        log.info("拦截的URI为:{}", URI);
        if(URI.contains("login")){
            return true;
        }
        String token = request.getHeader("token");
        if(token == null){
            response.setStatus(401);
            return false;
        }
        Claims claims = JwtUtil.parseClaims(token);
        if(claims == null){
            response.setStatus(401);
            return false;
        }
        log.info("claims为:{}",claims);
        return true;
    }
}
